package com.p2p.Androidp2p;

import java.util.ArrayList;
import java.util.HashMap;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * only works for getFileList
 * @author Chago
 *
 */
public class ParseHandler extends DefaultHandler
{
    boolean fList_tag = false;
    boolean file_tag = false;
    boolean name_tag = false;
    boolean fId_tag = false;
    boolean fSize_tag = false;
    boolean checksum_tag = false;

    ArrayList<HashMap<String, String>> data = null;
    HashMap<String, String> tMap = null;

    
    public ArrayList<HashMap<String,String>> getData()
    {
        return data;
    } 
    
    @Override 
    public void startDocument() throws SAXException 
    {
	//just for testing purposes
        data = new ArrayList<HashMap<String, String>>();
        tMap = new HashMap<String, String>();
        tMap.put("filename", "File Name");
        tMap.put("size", "Size");
        tMap.put("id", "ID");
        tMap.put("checksum", "checksum");
        data.add(tMap);
    }     
    
    public void startElement(String namespaceURI, String localName, 
            String qName, Attributes atts) throws SAXException 
    { 
       if (localName.equals("FileList")) 
       { 
            fList_tag = true; 
       }
       else if (localName.equals("File")) 
       { 
	   tMap = new HashMap<String, String>();
            file_tag = true; 
       }
       else if (localName.equals("Name")) 
       {
           name_tag = true; 
       }
       else if (localName.equals("ID")) 
       { 
           fId_tag = true; 
       }
       else if (localName.equals("Size")) 
       {
           fSize_tag = true; 
       }
       else if (localName.equals("Checksum")) 
       { 
           checksum_tag = true; 
       }
  }
    
    public void endElement(String namespaceURI, String localName, 
            String qName) throws SAXException 
    { 
       if (localName.equals("FileList")) 
       { 
            fList_tag = false; 
       }
       else if (localName.equals("File")) 
       {
	   data.add(tMap);
            file_tag = false; 
       }
       else if (localName.equals("Name")) 
       { 
            name_tag = false; 
       }
       else if (localName.equals("ID")) 
       { 
            fId_tag = false; 
       }
       else if (localName.equals("Size")) 
       { 
            fSize_tag = false; 
       }
       else if (localName.equals("Checksum")) 
       { 
            checksum_tag = false; 
       }
  } 
    
    @Override 
    public void characters(char ch[], int start, int length) 
    { 
	String insert = new String(ch, start, length);
          if(name_tag)
          {
              	tMap.put("filename", insert);
          }
          else if(fId_tag)
          {
              tMap.put("id", insert);
          }
          else if(fSize_tag)
          {
              tMap.put("size", insert);
          }
          else if(checksum_tag)
          {
              tMap.put("checksum", insert);
          }
    } 

  
}
